/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment1;

import java.io.*;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 *
 * @author cjaramillo
 */
public class Board {

    int m_rows;
    int m_columns;
    Cell m_terrain[][];
    Rover m_rover;
    int m_targetRow;
    int m_targetColumn;
    int m_roverRow;
    int m_roverColumn;
    
    Board(int rows, int columns) {
        this.rows(rows);
        this.columns(columns);
        m_terrain = new Cell[rows][columns];
        m_targetRow = 0;
        m_targetColumn = 0;
        m_roverRow = 0;
        m_roverColumn= 0;
        this.initBoard();
    }

    public void setRandomTerrain(Vector<String>fileLines)
    {
        for(int i=3; i<fileLines.size();i++)
        {
            StringTokenizer tokenizer = new StringTokenizer(fileLines.elementAt(i), ",");
            int column = Integer.parseInt(tokenizer.nextToken());
            int row = Integer.parseInt(tokenizer.nextToken());
            setCell(row, column, Cell.State.OBSTACLE);
        }
    }
    
    public void setRandomTerrain() throws IOException
    {
        System.out.println("Please enter percentage random terrain:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Integer wholePercentage = new Integer(br.readLine());
        double percentage = 0.0;
        if(wholePercentage.intValue() > 0)
        {
            percentage = (double)(wholePercentage.intValue())/100.0;
        }
        int randomSpaces = (int)(m_rows*m_columns*percentage);
        int createdSpaces = 0;
        Random generator = new Random();
        while(createdSpaces < randomSpaces)
        {
            int randomRow = generator.nextInt( m_rows );
            int randomColumn = generator.nextInt( m_columns );
            if(m_terrain[randomRow][randomColumn].state() != Cell.State.OBSTACLE)
            {
                setCell(randomRow, randomColumn, Cell.State.OBSTACLE);
                createdSpaces++;
            }
        }
        System.out.println("Created " + randomSpaces + " obstacles.");
        System.out.println("Board is currently:");
        this.showCurrentState();
    }
    private void rows(int rows) {
        m_rows = rows;
    }

    private void columns(int columns) {
        m_columns = columns;
    }

    public void rover(Rover rover)
    {
        m_rover = rover;
        m_terrain[rover.y()][rover.x()].setRover(m_rover);
        m_roverRow = rover.y();
        m_roverColumn = rover.x();
    }
    
    public void showCurrentState() {
        for (int x=0; x<m_rows; x++)
        {
            for(int y=0;y<m_columns;y++)
            {
                System.out.print(m_terrain[x][y]);
            }
            System.out.println("");
        }
    }

    private void initBoard() {
        for(int x=0; x<m_rows; x++)
            for(int y=0; y<m_columns;y++)
                m_terrain[x][y] = new Cell();
    }
    
    public void clearBoard() {
        for(int x=0; x<m_rows; x++)
            for(int y=0; y<m_columns;y++)
                m_terrain[x][y].setClear();
    }
    
    public void setCell(int row, int column, Cell.State state)
    {
        m_terrain[row][column].state(state);
    }
    
    public void setTarget(int row, int column)
    {
        m_targetRow = row;
        m_targetColumn = column;
        this.setCell(row,column,Cell.State.TARGET);
    }
    
    public int targetRow()
    {
        return m_targetRow;
    }
    
    public int targetColumn()
    {
        return m_targetColumn;
    }
    
    public int rows()
    {
        return m_rows;
    }
    
    public int columns()
    {
        return m_columns;
    }
    public Cell.State cellState(int row, int column)
    {
        return m_terrain[row][column].state();
    }

    void updateRoverPosition(Rover rover) {
        setCell(m_roverRow, m_roverColumn, Cell.State.CLEAR);
        this.rover(rover);
    }
}
